package com.ken.algorithms.tree;
import java.util.Scanner;

/**
 * <ul>
 * <li>Title: BinaryTreeTest</li>
 * <li>Description: 基础的二叉树练习 </li>
 * </ul>
 *
 * @author 程序员ken
 * @date 2021-08-26 10:25
 */
public class BinaryTreeTest {


    public static TreeNode scannerCreateTree() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入节点的值，逗号隔开，会按照顺序添加节点的");
        String next = sc.next();

        String[] split = next.split(",|，");
        TreeNode root = null;
        for (String str : split) {
            root = createTreeByParentNode(Integer.parseInt(str),root);
        }

        return root;
    }



    /**
     * 功能描述: 创建二叉树
     * @param value
     * @param parentNode
     * @return: algorithm.practice.Test.TreeNode
     * @author: 程序员ken
     * @date: 2021-08-26 10:22
     */
    private static TreeNode createTreeByParentNode(int value, TreeNode parentNode) {

        if(parentNode==null){
            parentNode = new TreeNode(value);
        }
        //当前值大于父节点 挂在右侧
        else if(parentNode.getVal()<value){
            if(parentNode.getRight()!=null){
                createTreeByParentNode(value,parentNode.getRight());
            }else{
                parentNode.setRight(new TreeNode(value));
            }
        }else{
            if(parentNode.getLeft()!=null){
                createTreeByParentNode(value,parentNode.getLeft());
            }else{
                parentNode.setLeft(new TreeNode(value));
            }
        }
        return  parentNode;
    }
}
